package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.Inv0004.Inv0004ExcuteInVO;
import com.ruoyi.buy.domain.entity.Inv0004.Inv0004ViewByIdVO;
import com.ruoyi.buy.domain.entity.PO.*;
import com.ruoyi.buy.mapper.PO.*;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.buy.service.IInv0004Service;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class Inv0004ServiceImpl implements IInv0004Service
{
    @Resource
    private BuyPurchasePostMapper buyPurchasePostMapper;

    @Resource
    private BuyPurchasePostItemMapper buyPurchasePostItemMapper;

    @Resource
    private BuyPurchaseBillMapper buyPurchaseBillMapper;

    @Resource
    private ICommonService commonService;

    @Resource
    private InvInboundBillMapper invInboundBillMapper;

    @Resource
    private BuyPurchaseBillItemMapper buyPurchaseBillItemMapper;

    @Resource
    private InvInboundBillItemMapper invInboundBillItemMapper;
    /**
     * 查询采购邮寄列表
     *
     * @param buyPurchasePost 采购邮寄
     * @return 采购邮寄
     */
    @Override
    public List<BuyPurchasePost> selectBuyPurchasePostList(BuyPurchasePost buyPurchasePost)
    {
        return buyPurchasePostMapper.selectBuyPurchasePostList(buyPurchasePost);
    }

    @Override
    public Inv0004ViewByIdVO viewById(BuyPurchasePost buyPurchasePost) {
        Inv0004ViewByIdVO result = new Inv0004ViewByIdVO();
        BuyPurchasePost billPO = this.buyPurchasePostMapper.selectBuyPurchasePostById(buyPurchasePost.getId());
        BuyPurchasePostItem buyPurchasePostItem = new BuyPurchasePostItem();
        buyPurchasePostItem.setnMainId(buyPurchasePost.getId());
        List<BuyPurchasePostItem> itemList = this.buyPurchasePostItemMapper.selectBuyPurchasePostItemList(buyPurchasePostItem);
        result.setBuyPurchasePost(billPO);
        result.setItemPOList(itemList);
        return result;
    }

    @Override
    public int arrivalComfirm(BuyPurchasePost buyPurchasePost) {
        buyPurchasePost.setUpdateTime(DateUtils.getNowDate());
        buyPurchasePost.setStatus("01");
        return buyPurchasePostMapper.updateBuyPurchasePost(buyPurchasePost);
    }

    @Override
    public int executeIn(Inv0004ExcuteInVO para) throws Exception {
        BuyPurchasePost bill = para.getPostBill();

        //查合同相关信息
        BuyPurchaseBill buyPurchaseBill = new BuyPurchaseBill();

        //1.先查邮寄主表，要合同编号信息
        String vContractNos =  bill.getvContractNos();
        String[] vContractNoList = vContractNos.split(",");

        ArrayList< String> arrayList = new ArrayList<String>(vContractNoList.length);
        Collections.addAll(arrayList, vContractNoList);

        for (String vContractNo:arrayList
        ) {
            buyPurchaseBill = new BuyPurchaseBill();
            buyPurchaseBill.setvBillNo(vContractNo);
            List<BuyPurchaseBill> buyPurchaseBillList = this.buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill);
            buyPurchaseBill = buyPurchaseBillList.get(0);
            BuyPurchaseBillItem buyPurchaseBillItem = new BuyPurchaseBillItem();
            buyPurchaseBillItem.setnMainId(buyPurchaseBill.getId());
            List<BuyPurchaseBillItem> buyPurchaseBillItems = this.buyPurchaseBillItemMapper.selectBuyPurchaseBillItemList(buyPurchaseBillItem);

            //补全主表信息
            InvInboundBill invInboundBill = new InvInboundBill();
            String vBillNo = commonService.getNo("RK");
            invInboundBill.setvBillNo(vBillNo);
            invInboundBill.setIotype("I");
            invInboundBill.setvType("ICG");
            invInboundBill.setvCorBillNo(bill.getvBillNo());
            invInboundBill.setdExecute(DateUtils.getNowDate());
            invInboundBill.setvContractNo(buyPurchaseBill.getvBillNo());
            invInboundBillMapper.insertInvInboundBill(invInboundBill);

            //反查主表ID
            invInboundBill.setdExecute(null);
            List<InvInboundBill> list = this.invInboundBillMapper.selectInvInboundBillList(invInboundBill);
            Long nMainId = list.get(0).getId();

            //遍历子表保存
            List<InvInboundBillItem> itemList = para.getItemPOList();
            for (InvInboundBillItem item :itemList
            ) {
                //补全子表信息
                item.setnMainId(nMainId);
                List<BuyPurchaseBillItem> result = buyPurchaseBillItems.stream().filter(x ->  x.getvSpaName().equals(item.getvSpaName())
                        && x.getvSpaBrand().equals(item.getvSpaBrand()) && x.getvSpaBrand().equals(item.getvSpaBrand())).collect(Collectors.toList());
                if (result.isEmpty()){
                    continue;
                }
                item.setnPrice(result.get(0).getnPrice());
                item.setvProjectNo(buyPurchaseBill.getvProjectNo());
                item.setvProjectDec(buyPurchaseBill.getvProjectDec());
                this.invInboundBillItemMapper.insertInvInboundBillItem(item);

                //更新合同明细已入库数量
                BuyPurchaseBillItem updateData = result.get(0);
                if(null == updateData.getnINWHQty()){
                    updateData.setnINWHQty(item.getnQty());
                }else{
                    updateData.setnINWHQty(item.getnQty().add(updateData.getnINWHQty()));
                }
                this.buyPurchaseBillItemMapper.updateBuyPurchaseBillItem(updateData);
            }
            //反查明细
            InvInboundBillItem searchItem = new InvInboundBillItem();
            searchItem.setnMainId(nMainId);
            List<InvInboundBillItem> listResult = this.invInboundBillItemMapper.selectInvInboundBillItemList(searchItem);
            // 更改物流状态
            bill.setStatus("03");
            this.buyPurchasePostMapper.updateBuyPurchasePost(bill);
            // 更改库存
            commonService.changeInventory(list.get(0), new ArrayList<>(listResult),"IN");
        }

        return 1;
    }

    @Override
    public Inv0004ViewByIdVO viewRKBillById(BuyPurchasePost buyPurchasePost) {
        Inv0004ViewByIdVO result = new Inv0004ViewByIdVO();
        InvInboundBill invInboundBill = new InvInboundBill();
        invInboundBill.setvCorBillNo(buyPurchasePost.getvBillNo());
        List<InvInboundBill> list = this.invInboundBillMapper.selectInvInboundBillList(invInboundBill);
        if(!list.isEmpty()){
            result.setInvInboundBill(list.get(0));
            InvInboundBillItem invInboundBillItem = new InvInboundBillItem();
            invInboundBillItem.setnMainId(list.get(0).getId());
            List<InvInboundBillItem> invInboundBillItemList = this.invInboundBillItemMapper.selectInvInboundBillItemList(invInboundBillItem);
            result.setInvInboundBillItemList(invInboundBillItemList);
        }
        return result;
    }
}
